
package com.googlecode.animeclassifier.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.googlecode.animeclassifier.AnimeManager;

/** 
 * TableDemo is just like SimpleTableDemo, except that it
 * uses a custom TableModel.
 */
public class MainPanel extends JPanel {
    private static boolean DEBUG = false;
    AnimeManager animeManager;
	private BrowsePanel browsePanel;
	private ClassifyPanel classifyPanel;

    public MainPanel(final AnimeManager animeManager) {
        super(new BorderLayout());

        this.animeManager = animeManager;
        	
        add(setupRootPanel(), BorderLayout.NORTH);
        add(setupTabbedPane());
    }
    
    JComponent setupRootPanel() {
    	JPanel panel = new JPanel();
    	JLabel label = new JLabel("Anime Root: ");
    	JTextField text = new JTextField(animeManager.getRoot().getAbsolutePath(), 80);
    	JButton button = new JButton("Browse");
    	
    	panel.setLayout(new FlowLayout());
        button.addActionListener(new FileChooserButton(button, text));
        
        panel.add(label);
        panel.add(text);
        panel.add(button);
        
        return panel;
    }
    
    protected class FileChooserButton implements ActionListener {
    	JButton button;
    	JTextField field;
	    JFileChooser animeRoot;

    	public FileChooserButton(JButton button, JTextField field) {
    		this.button = button;
    		this.field = field;
	        animeRoot = new JFileChooser(animeManager.getRoot());
	        animeRoot.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    	}
    	
		@Override
		public void actionPerformed(ActionEvent e) {
			int returnVal = animeRoot.showOpenDialog(null);

	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = animeRoot.getSelectedFile();
	            //This is where a real application would open the file.
	            field.setText(file.getAbsolutePath());
	            System.out.println("Opening: " + file.getAbsolutePath() + ".");
	            animeManager.setRoot(file.getAbsolutePath());
	            browsePanel.repaint();
	        } else {
	        	System.out.println("Open command cancelled by user.");
	        }
		}
    	
    }
    

    
    JTabbedPane setupTabbedPane() {
    	JTabbedPane tabbedPane = new JTabbedPane();

        tabbedPane.addTab("List", null, browsePanel = new BrowsePanel(this, animeManager),
                          "Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        tabbedPane.addTab("Classify", null, classifyPanel = new ClassifyPanel(animeManager),
                          "Does twice as much nothing");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

        JComponent panel3 = makeTextPanel("Anime Classifier\nhttp://code.google.com/p/anime-classifier/");
        tabbedPane.addTab("About", null, panel3,
                          "Still does nothing");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        
		return tabbedPane;
    }
    
    
    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public static void createAndShowGUI(AnimeManager animeManager) {
        //Create and set up the window.
        JFrame frame = new JFrame("Anime Classifier");
        
        frame.setJMenuBar(setupMenuBar());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MainPanel newContentPane = new MainPanel(animeManager);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    private static JMenuBar setupMenuBar() {
    	JMenuBar menuBar = new JMenuBar();
    	JMenu file = new JMenu("File");
        menuBar.add(file);
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("WOW");
			}
        	
        });
        
        file.add(openItem);
        file.add(new JMenuItem("Close"));
        
    	JMenu classify = new JMenu("Classify");
        menuBar.add(classify);
        classify.add(new JMenuItem("Move"));
        
    	return menuBar;
    }
}